package com.example.administrator.caigou51.request;


import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.basic.SuperDIYSwipeRefreshLayout;
import com.example.administrator.caigou51.recyclerCard.basic.SuperSwipeRefresh;
import com.loopj.android.http.RequestParams;

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;

import org.apache.http.protocol.HTTP;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 后台接口管理
 *
 * @author xyg
 */
public class RequestServerManager implements IRequestServer {

    private static RequestServerManager instance;
    private IRequestServer iRequestServer;

    private RequestServerManager(IRequestServer iRequestServer) {
        this.iRequestServer = iRequestServer;
    }

    public static RequestServerManager getInstance() {
        if (instance == null) {
            instance = new RequestServerManager(RequestServer.getInstance());
        }
        return instance;
    }

    public static RequestServerManager getInstanceDIY() {
        if (instance == null) {
            instance = new RequestServerManager(RequestServerDIY.getInstance());
        }
        return instance;
    }
    //有动态参数才调用

    /**
     * @param key
     * @param unKnowValueMap       map文件数组
     * @param unKnowValueMapString map字符数组
     * @param paramsValue          普通字符串
     * @return
     */
    public RequestParams getRequestParams(String key, HashMap<String, File> unKnowValueMap, HashMap<String, String> unKnowValueMapString, String... paramsValue) {
        //上传图片用到unKnowValueMap，动态添加进去,没有就传个nul

        RequestParams params = new RequestParams();
        String[] values = Constant.paramsMap.get(key);

        if (values != null) {
            int i = 0;
            for (; i < paramsValue.length; i++) {
                params.put(values[i], paramsValue[i]);
            }
        }
        if (unKnowValueMapString != null) {
            Iterator<?> iter = unKnowValueMapString.entrySet().iterator();
            while (iter.hasNext()) {
                Entry entry = (Entry) iter.next();
                try {
                    String s = entry.getValue().toString();
                    params.put(entry.getKey().toString(), s);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        if (unKnowValueMap != null) {
            Iterator<?> iter = unKnowValueMap.entrySet().iterator();
            while (iter.hasNext()) {
                Entry entry = (Entry) iter.next();
                File f = (File) entry.getValue();
                if (f != null && f.exists()) {
                    try {
                        //File filew[]=new File[]{f,f,f};
                        final String contentType = RequestParams.APPLICATION_OCTET_STREAM;
                        params.put(entry.getKey().toString(),f, contentType, f.getName());
                        //  params.put("fileOne", f, contentType, "fileOne");
                        //  params.put("fileTwo", f, contentType,entry.getKey().toString());
                        //  params.put("uploadedFile", f, contentType, "customFileThree");
                        //   params.put("uploadedFile", f, "multipart/form-data",entry.getKey().toString());
                        //   params.put("uploadedFile",filew, contentType);

                        //File filew[]=new File[]{f,f,f};
                        //params.put( entry.getKey().toString()+"_1", f);
                        //params.put( entry.getKey().toString()+"_2", f);
                        params.setHttpEntityIsRepeatable(true);
                        params.setUseJsonStreamer(false);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

//		System.out.println(key+params.toString());
        return params;
    }

    /**
     * 有P 又有T的
     *
     * @param key
     * @param paramsValue
     * @return
     */
    public RequestParams getRequestParams(String key, String... paramsValue) {
        //上传图片用到unKnowValueMap，动态添加进去,没有就传个nul

        RequestParams params = new RequestParams();
        String[] values = Constant.paramsMap.get(key);

        if (values != null) {
            //params.put("t", key);
            int i = 0;
            for (; i < paramsValue.length; i++) {
                params.put(values[i], paramsValue[i]);
            }
        }
        System.out.println("key======" + params.toString());
        return params;
    }

    /**
     * 只有P的
     *
     * @param key
     * @param paramsValue
     * @return
     */
    public RequestParams getRequestParamsP(String key, String... paramsValue) {
        //上传图片用到unKnowValueMap，动态添加进去,没有就传个nul

        RequestParams params = new RequestParams();
        /*String[] values = paramsMap.get(key);

    	if(values != null){
//    		params.put("t", key);
    		int i = 0;
    		for (; i < paramsValue.length; i++) {
    			params.put(values[i], paramsValue[i]);
    		}
    	}
    	*/
        return params;
    }






    @Override
    public <T extends IBaseResponse> void handleMethod(
            GBaseActivity gBaseActivity, MaterialListView materialListView,
            SuperSwipeRefresh swipeRefresh, boolean isLoadMore,
            String url, String tokenAct, RequestParams requestParams, Class<T> instanceClass,
            ICallBackForRequestServer<T> callBack) {
        iRequestServer.handleMethod(gBaseActivity, materialListView, swipeRefresh, isLoadMore, url, tokenAct, requestParams, instanceClass, callBack);
    }


    @Override
    public <T extends IBaseResponse> void handleMethodGet(
            GBaseActivity gBaseActivity, MaterialListView materialListView,
            SuperSwipeRefresh swipeRefresh, boolean isLoadMore,
            String url, String tokenAct, RequestParams requestParams, Class<T> instanceClass,
            ICallBackForRequestServer<T> callBack) {
        iRequestServer.handleMethodGet(gBaseActivity, materialListView, swipeRefresh, isLoadMore, url, tokenAct, requestParams, instanceClass, callBack);
    }



    @Override
    public <T extends IBaseResponse> void handleMethodDIY(GBaseActivity gBaseActivity, MaterialListView materialListView, boolean isLoadMore, SuperDIYSwipeRefreshLayout swipeRefresh, String url, String tokenAct, RequestParams requestParams, Class<T> instanceClass, ICallBackForRequestServer<T> callBack) {
        iRequestServer.handleMethodDIY(gBaseActivity, materialListView,isLoadMore, swipeRefresh, url, tokenAct, requestParams, instanceClass, callBack);
    }


}
